<?php


namespace App\Http\Controllers\Agent;


use App\Http\Controllers\Controller;
use App\Utils\Result;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class UploadController extends Controller
{
    public function upload(Request $request): \Illuminate\Http\JsonResponse
    {
        $file = $request->file('file');
        if (!$file) {
            return Result::error(400, '请选择上传的文件');
        }
        $filePath = 'storage/' . date('Ymd') . '_' . Str::random(16);
        $newFileName = $file->getClientOriginalName();

        if (!file_exists(public_path($filePath))) {
            mkdir(public_path($filePath));
        }
        $file->move(public_path($filePath), $newFileName);

        return Result::success([
            'url' => env('APP_URL') . '/' . $filePath . '/' . $newFileName
        ]);
    }
}
